package com.yhb.service;

import com.yhb.model.config.geo.GeoConfig;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import me.edzh.spring.bootstrap.utils.JSONUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Data
public class ConfigFileService {

    private GeoConfig yinShengAddressRelationShip;
    private HashMap<String, String> yinShengBankType;
    private List<String> yinShengBlackBankList;

    @Autowired
    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    private CommissionService commissionService;

    @Autowired
    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    private GenIPService genIPService;


    @PostConstruct
    public void init() throws Throwable {
        String addressContent = FileUtils.readFileToString(new File("/data/addresses/yinsheng_address_relationship.json"), StandardCharsets.UTF_8);
        this.yinShengAddressRelationShip = JSONUtil.parseJson(addressContent, GeoConfig.class);
        Map yinshengConfig = JSONUtil.parseJson(FileUtils.readFileToString(new File("/data/bank/yinsheng_bank.json"), StandardCharsets.UTF_8), HashMap.class);
        this.yinShengBankType = (HashMap<String, String>)yinshengConfig.get("supported");
        this.yinShengBlackBankList = (List<String>)yinshengConfig.get("blackList");

        commissionService.updateRate(FileUtils.readFileToString(new File("/data/config/feeRate.yaml"), StandardCharsets.UTF_8));
        genIPService.updateRanges(FileUtils.readFileToString(new File("/data/config/geoIPs.yaml"), StandardCharsets.UTF_8));
    }


}
